﻿using UnityEngine;
using System.Collections;
using DICE_GS;

public class ChooseGameOrderWindow : LTEventListenerC {
	public GameObject[] orderCardObjects;

	public UIProgressBar countDownSlider;
	float timerRatio;

	// Use this for initialization
	void Start () {
		for(int i=0; i< orderCardObjects.Length; i++)
			orderCardObjects[i].SetActive(false);

		for(int i=0; i<BoardManager.Instance.boardPersonPCList.Count; i++)
		{
			orderCardObjects[i].SetActive(true);
		}

		// position fix by runner count
		if(BoardManager.Instance.boardPersonPCList.Count==2)
		{
			for(int i=0; i<BoardManager.Instance.boardPersonPCList.Count; i++)
			{
				Vector3 newPos = orderCardObjects[i].transform.localPosition;
				newPos.x += 244;
				orderCardObjects[i].transform.localPosition = newPos;
			}
		}

		if(BoardManager.Instance.boardPersonPCList.Count==3)
		{
			for(int i=0; i<BoardManager.Instance.boardPersonPCList.Count; i++)
			{
				Vector3 newPos = orderCardObjects[i].transform.localPosition;
				newPos.x += 125;
				orderCardObjects[i].transform.localPosition = newPos;
			}
		}
		timerRatio=1;
		StartCoroutine("CoTimer");
	}

	int selectedCardIndex = -1;
	public override void OnEvent (int eventID)
	{
		base.OnEvent (eventID);

		selectedCardIndex = eventID;
//		FlipOrderCard(eventID, false, false);
//		orderCardObjects[eventID].GetComponent<Animator>().Play("OrderCardFX");
//		GameObject child = orderCardObjects[eventID].transform.Find("OrderCard").gameObject;
//		if(child != null)
//		{
//			Debug.LogError("child is not null");
//			child.GetComponent<Animator>().Play("OrderCard");
//		}

//		foreach(GameObject obj in orderCardObjects)
//			obj.collider.enabled = false;

		CSceneManager.manager.SendSelectedGameOrder(eventID);
	}

	public void FlipFirstOrderCard(int[] resultOrderList)
	{
		for(int i=0; i<BoardManager.Instance.boardPersonPCList.Count; i++)
		{
			if( orderCardObjects[i].GetComponent<GameOrderCard>().isMe )
			{
				orderCardObjects[i].GetComponent<Animator>().Play("OrderCardFX");
				GameObject child = orderCardObjects[i].transform.Find("OrderCard").gameObject;
				if(child != null)
				{
					Debug.LogError("child is not null");
					child.GetComponent<Animator>().Play("OrderCard");
				}
				CSkillData skillData = CSkillDataManager.Instance.FindSkillData(orderCardObjects[i].GetComponent<GameOrderCard>().myTempSkillId);
				if( skillData != null )
				{
					_myTempSkillId = orderCardObjects[i].GetComponent<GameOrderCard>().myTempSkillId;
					Debug.LogError(_myTempSkillId);
					orderCardObjects[i].GetComponent<GameOrderCard>().chanceSkillIcon.spriteName = skillData.icon_name;
				}
			}
			else
			{
				orderCardObjects[i].GetComponent<Animator>().Play("OrderCardFX_enemy");
				GameObject child = orderCardObjects[i].transform.Find("OrderCard").gameObject;
				if(child != null)
				{
					Debug.LogError("child is not null");
					child.GetComponent<Animator>().Play("OrderCard_enemy");
				}
			}
			if( resultOrderList[i] == 1 )
				orderCardObjects[i].GetComponent<GameOrderCard>().gameOrderImage.spriteName = "Text_1st";
			else if( resultOrderList[i] == 2 )
				orderCardObjects[i].GetComponent<GameOrderCard>().gameOrderImage.spriteName = "Text_2nd";
			else if( resultOrderList[i] == 3 )
				orderCardObjects[i].GetComponent<GameOrderCard>().gameOrderImage.spriteName = "Text_3rd";
			else if( resultOrderList[i] == 4 )
				orderCardObjects[i].GetComponent<GameOrderCard>().gameOrderImage.spriteName = "Text_4th";			
		}
		StopCoroutine("CoTimer");
		StartCoroutine("Co_HideWindow");
	}

	public void SetMySelectedCardState()
	{
		orderCardObjects[selectedCardIndex].GetComponent<GameOrderCard>().mySelectedMark.SetActive(true);
	}

	public void DisableOtherCard(int index)
	{
		orderCardObjects[index].GetComponent<GameOrderCard>().alreadySelectedMark.gameObject.SetActive(true);
		orderCardObjects[index].collider.enabled = false;
	}

	public void FlipOrderCard(int index, bool isFailed, bool isFirst)
	{
		orderCardObjects[index].GetComponent<GameOrderCard>().isFirst = isFirst;
		orderCardObjects[index].GetComponent<GameOrderCard>().isFailed = isFailed;

		if( !orderCardObjects[index].GetComponent<Animator>().enabled )
		{
			orderCardObjects[index].GetComponent<Animator>().enabled = true;
			orderCardObjects[index].collider.enabled = false;
		}
	}

	int _myTempSkillId = 0;
	IEnumerator Co_HideWindow()
	{
		yield return new WaitForSeconds(2f);

		HudManager.Instance.AchieveTempSkill(BoardManager.Instance.boardPerson_me, _myTempSkillId, true);

		gameObject.SetActive(false);
	}

	public void ResetChooseStatus()
	{
		foreach(GameObject obj in orderCardObjects)
		{
			if( !obj.GetComponent<Animator>().enabled )
				obj.collider.enabled = true;
		}
	}

	public void SetCountDownSlider(float sec)
	{
		timerRatio = sec/20.0f;
	}

	IEnumerator CoTimer()
	{
		while(timerRatio>0)
		{
			timerRatio -= Time.deltaTime/20.0f;
			countDownSlider.value = timerRatio;
			yield return null;
		}
		countDownSlider.value=0;
		glog.debug("timer Ended");
	}
}
